package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbCpuStandardDeviceStandardRelate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbCpuStandardDeviceStandardRelateRepository extends JpaRepository<CmdbCpuStandardDeviceStandardRelate, Integer>, JpaSpecificationExecutor<CmdbCpuStandardDeviceStandardRelate> {
    List<CmdbCpuStandardDeviceStandardRelate> findAllByIdIn(List<Long> id);

    @Query(value = "select m from CmdbCpuStandardDeviceStandardRelate m where m.isDelete=0 " +
            "and (:#{#paramSearchDto.deviceStandardCode} is null OR m.deviceStandardCode= :#{#paramSearchDto.deviceStandardCode}) " +
            "and (:#{#paramSearchDto.cpuStandardCode} is null OR m.cpuStandardCode= :#{#paramSearchDto.cpuStandardCode}) " +
            "and (:#{#paramSearchDto.amount} is null OR m.amount= :#{#paramSearchDto.amount}) " +
            "")
    Page<CmdbCpuStandardDeviceStandardRelate> searchByParam(Pageable pageable, CmdbCpuStandardDeviceStandardRelate paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_cpu_standard_device_standard_relate set is_delete = :deleted where id in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<Integer> codes);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_cpu_standard_device_standard_relate set is_delete = :deleted where device_standard_code = :deviceStandardCode ", nativeQuery = true)
    void updateIsDeleteByDeviceStandardCode(Integer deleted,String deviceStandardCode);

    CmdbCpuStandardDeviceStandardRelate findOneByDeviceStandardCodeAndIsDelete(String deviceStandardCode, Integer isDelete);

//    @Modifying
//    @Query(value = "update t_cmdb_cpu_standard_device_standard_relate set is_delete = :deleted where id in :codes ", nativeQuery = true)
//    void updateIsDeleteByDeviceStandardCode(Integer deleted,String cpuStandardCode);
}

